package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.KindItem;
import cn.wolfcode.crm.query.KindQueryObject;
import cn.wolfcode.crm.service.IKindItemService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author 雷德望
 */
@Controller
@RequestMapping("kinditem")
public class KindItemController {
    @Autowired
    private IKindItemService service;
    
    @RequiresPermissions("kindItem:index")
    @PermissionName("支出明细")
    @RequestMapping("")
    public String index(){
        return "kinditem/list";
    }
    @RequestMapping("listLine")
    public String listLine(){


        return "kinditem/listLine";
    }

    @RequestMapping("listSave")
    public String listSave(){
        return "kinditem/listSave";
    }

    @RequiresPermissions("kindItem:list")
    @PermissionName("支出明细")
       @RequestMapping("list")
    @ResponseBody
    public List<KindItem> list(){
        return service.list();
    }

    @RequestMapping("listView")
    @ResponseBody
    public List<KindItem> listView( KindQueryObject qo ){
        return service.listView(qo);
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(@ModelAttribute("qo") KindQueryObject qo ,Model model){

        return service.query(qo);
    }
    @RequestMapping("queryForMoney")
    @ResponseBody
    public int queryForMoney(){

        return service.queryForMoney();
    }

    @RequiresPermissions("kindItem:save")
    @PermissionName("支出录入")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(KindItem entity){
    	JsonResult result = new JsonResult();
    	try {
    	    if(entity.getKdate() == null){
                entity.setKdate(new Date());
            }
    		service.save(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("保存失败");
		}
        return result;
    }
    
    @RequiresPermissions("kindItem:update")
    @PermissionName("支出更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(KindItem entity){
    	JsonResult result = new JsonResult();
    	try {

    		service.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("更新失败" + e.getMessage());
		}
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("kindItem:delete")
    @PermissionName("支出明细删除")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            service.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("删除操作失败" + e.getMessage());
        }
        return result;
    }


}
